import os
import time
import newegg
#import graph # I don't use Forrest's graphing
import best
import pickle

types = [
    ('sata', {None: (2010150014, '359:15133', '359:7918')}),
    ('satas', {None: (2010150380, '359:15133', '359:7918')}),
    ('usb', {None: (2013240522,)}),
    ('ssd', {None: (2010150636,)}),
    ('enclosure', {None: (2010150414, '359:20386', '359:15635', '359:47642', '359:28161', '359:28610', '359:32416', '359:7839', '359:7851', '360:7801', '360:7802')}),
    ('flash', {
        "SD": (2010170068, '531:17056', '531:7930', '531:25651'),
        "miniSD": (2010170068, '531:25687', '531:14916'),
        "microSD": (2010170068, '531:31144', '531:18914'),
        "CF": (2010170068, '531:7923'),
    }),
]

#result_dir = os.path.join(os.path.dirname('__file__'), "result")
result_dir = os.path.join(os.getcwd().strip(), "result")
print "Saving results in: " + result_dir
for name, subtypes in types:
    t = time.time()
    
    print '\nScraping: ' + name
    items = dict((subname, list(newegg.get(*call))) for subname, call in subtypes.iteritems())

    #save the data for later examiniation
    try: 
        f = open(os.path.join(result_dir, str(t) + "_" + name + '_record.pkl'), "w")
        pickle.dump(items,f)
        f.close()
        print "Type: " + name + " was pickled."
    except:
        f.close()
        print "Error pickling type: " + name 
        continue
    
    #but we want our data now...
    #make a tab delimited file
    try:
        f = open(os.path.join(result_dir, str(t) + "_" + name + '_record.txt'), "w")
        f.write(u"TotalRatings\t1eggs\t2eggs\t3eggs\t4eggs\t5eggs\tAvgEggs\tBestRatingOrder\tUnixTimestamp\tSize(GB)\tPrice\tPriceShip\tDescription\tLink\tGB/$\tGB/$ incl sh\n")
        #print type
        #print "len type: " + str(len(items))
        for subname, itemz in items.iteritems():
            print "len itemz: " + str(len(itemz))
            for item in itemz:
                #print "Item " + str(item)
                s = ""
                for x in item:
                    #print x
                    s += str(x).strip() + '\t'
                f.write(((s).strip() + '\n').encode("UTF-8"))
        print "Type " + name + " was logged as tab delimited text file."
    except:
        f.close()
        print "Error writing tab delimited file for " + name
    
##    print 'Graphing'
##    graph.graph(dict((k, [(item[0], item[0]/item[1]) for item in v]) for k, v in items.iteritems()), os.path.join(result_dir, name + '.png'))
##    
    print 'Itemizing'
    best.best(name, items, os.path.join(result_dir, name + '.txt'), t, os.path.join(result_dir, 'record.txt'))
    print "Finished processing all types."
